package com.xyh.flowershop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyh.flowershop.domain.Type;
import com.xyh.flowershop.mapper.TypeMapper;
import com.xyh.flowershop.service.TypeService;
import com.xyh.flowershop.vo.resp.common.KV;
import com.xyh.flowershop.vo.resp.index.TypeIndexRespVo;
import com.xyh.flowershop.vo.resp.type.TypePageRespVo;
import com.xyh.flowershop.vo.req.type.TypeEditReqVo;
import com.xyh.flowershop.vo.req.type.TypePageReqVo;
import com.xyh.flowershop.vo.resp.type.TypeRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {
    @Override
    public List<TypeIndexRespVo> getTypeList() {
        LambdaQueryWrapper<Type> typeWrapper = new LambdaQueryWrapper<>();
        typeWrapper.eq(Type::getHaveChild,false)
                .eq(Type::getDeleted, false);
        List<Type> types = baseMapper.selectList(typeWrapper);
        List<TypeIndexRespVo> vos =  types.stream().map(item ->{
            TypeIndexRespVo vo = new TypeIndexRespVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).toList();
        return vos;
    }

    @Override
    public Boolean addType(TypeEditReqVo vo) {
        Type type = new Type();
        BeanUtils.copyProperties(vo, type);
        type.setCreateTime(new Date());
        type.setUpdateTime(new Date());
        return baseMapper.insert(type) > 0;
    }

    @Override
    public Boolean editType(TypeEditReqVo vo) {
        Type type = baseMapper.selectById(vo.getTypeId());
        type.setUpdateTime(new Date());
        BeanUtils.copyProperties(vo, type);
        return baseMapper.updateById(type) > 0;
    }

    @Override
    public Boolean deleteType(Long id) {

        return baseMapper.removeType(id) > 0;
    }

    @Override
    public Boolean deleteBatchType(List<Long> ids) {
        if (ids == null || ids.size() <= 0){
            return false;
        }
        return baseMapper.removeBatchType(ids) > 0;
    }

    /**
     * 只考虑到二级分类
     * @param vo
     * @return
     */
    @Override
    public IPage<TypePageRespVo> getPageType(TypePageReqVo vo) {
        IPage<TypePageRespVo> pages = new Page<>(vo.getPageIndex(), vo.getPageSize());
        pages = baseMapper.selectPageByCondition(pages, vo);
//        pages.setRecords(getChildren(pages.getRecords()));
        return pages;
    }

    @Override
    public TypeRespVo getType(Long id) {
        return baseMapper.selectTypeById(id);
    }

    @Override
    public List<KV> selectTypeKV() {
        return baseMapper.selectTypeKV();
    }

/*
    private List<TypePageRespVo> getChildren(List<TypePageRespVo> vos){
        Map<String, Object> conditionMap = new ConcurrentHashMap<>();
        List<TypePageRespVo> voList = new ArrayList<>();
        // 父找子
        Iterator<TypePageRespVo> iterator = vos.iterator();
        if(iterator.hasNext()){
            TypePageRespVo vo = iterator.next();
            if(vo.getHaveChild()){
                conditionMap.put("parentId", vo.getTypeId());
                conditionMap.put("id", 0);
                conditionMap.put("typeName", "");
                List<TypePageRespVo> respVoList = baseMapper.selectListByCondition(conditionMap);
                vo.setChildren(respVoList);
                voList.add(vo);
            }else { // 子找父
                conditionMap.put("id",vo.getParentTypeId());
                conditionMap.put("typeName", "");
                conditionMap.put("parentId", 0);
                List<TypePageRespVo> list = baseMapper.selectListByCondition(conditionMap);
                TypePageRespVo respVo = list.get(0);
                if(voList.contains(respVo)){
                    respVo.getChildren().add(vo);
                }else {
                    List<TypePageRespVo> children = new ArrayList<>();
                    children.add(vo);
                    respVo.setChildren(children);
                    voList.add(respVo);
                }
            }
            conditionMap.clear();
            iterator.remove();
        }
        return voList;
    }
*/

}
